package com.cie2.map.services;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.cie2.map.entities.User;
import com.cie2.map.entities.UserProfile;
import com.cie2.map.entities.User.AccessLevel;

public class UserManagement {

	static Logger logger = 
		Logger.getLogger(UserManagement.class.getName());
	
	private Session session;

		
	public UserManagement(Session session) {
		this.session = session;
	}
	
	public void addUser(User user, AccessLevel userAccessLevel) {
    	
    	UserProfile userProfile = new UserProfile();
//    	userProfile.setUser(user);
    	userProfile.setAccessLevel(userAccessLevel);
    	user.setCurrentProfile(userProfile);

    	this.session.persist(userProfile);
    	this.session.persist(user);

	}
	
	public User load(Long id) {
		return (User) this.session.load(User.class, id);    
	}
	
	public List <User> getUsers() {
		List <User> userList = this.session.createCriteria(User.class).list();
		return userList;
	}
	
	public void deleteUser(Long id) {
		// can't we just delete it ? 
		 User user = (User) this.session.load(User.class, id);
		 if(user.getCurrentProfile() != null)
			 this.session.delete(user.getCurrentProfile());		 

		 this.session.delete(user);

	}
	
	public void updateUser(User user, AccessLevel userAccessLevel) {
		UserProfile profile = user.getCurrentProfile();
		profile.setAccessLevel(userAccessLevel);
		session.persist(profile);
    	this.session.persist(user);		
	}
	
	private void populateUserTask() {
		//ambil task yang belum di vote. add to user task
	}
	
	@SuppressWarnings("unchecked")
	public List <User> getActiveUser() {
		return this.session.createCriteria(User.class).list();
	}
}
